package org.meb.speedway.dao;

import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.meb.speedway.model.registry.Team;
import org.meb.speedway.model.registry.Team_;

public class TeamDao extends BaseDao<Team> {

	public Team findByName(String name) {
		CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
		CriteriaQuery<Team> cq = cb.createQuery(Team.class);
		Root<Team> root = cq.from(Team.class);
		cq.where(cb.equal(root.get(Team_.name), name));
		Team result;
		try {
			result = getEntityManager().createQuery(cq).getSingleResult();
		} catch (NoResultException e) {
			result = null;
		}
		return result;
	}
}
